package com.hupu.jrs.ui.forum

import android.os.Bundle
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.View
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersDecoration
import javax.inject.Inject
import butterknife.BindView
import butterknife.ButterKnife
import com.hupu.jrs.R
import com.hupu.jrs.ui.BaseFragment
import com.hupu.jrs.ui.main.MainComponent
import com.hupu.jrs.ui.thread.list.ThreadListActivity

/**
 * Created by sll on 2016/3/11.
 */
class ForumListFragment : BaseFragment(), ForumListContract.View, ForumListAdapter.OnItemClickListener {

    @BindView(R.id.recyclerView)
    internal var recyclerView: RecyclerView? = null
    @BindView(R.id.refreshLayout)
    internal var refreshLayout: SwipeRefreshLayout? = null

    private var forumId: String? = null

    @Inject
    internal var mPresenter: ForumListPresenter? = null
    @Inject
    internal var mAdapter: ForumListAdapter? = null

    override fun initInjector() {
        getComponent(MainComponent::class.java).inject(this)
    }

    override fun initContentView(): Int {
        return R.layout.base_list_layout
    }

    override fun getBundle(bundle: Bundle) {
        forumId = bundle.getString("forumId")
    }

    override fun initUI(view: View) {
        ButterKnife.bind(this, view)
        mPresenter!!.attachView(this)
        val layoutManager = LinearLayoutManager(getActivity().getApplicationContext())
        recyclerView!!.layoutManager = layoutManager
        recyclerView!!.adapter = mAdapter
        val headersDecor = StickyRecyclerHeadersDecoration(mAdapter)
        recyclerView!!.addItemDecoration(headersDecor)
        refreshLayout!!.isEnabled = false
        mAdapter!!.setOnItemClickListener(this)
    }

    override fun initData() {
        mPresenter!!.onForumListReceive(forumId.toString())
    }

    override fun showLoading() {
        showProgress(true)
    }

    override fun hideLoading() {
        showContent(true)
    }

    override fun onError() {
        showError(true)
    }

    override fun renderForumList(forumList: MutableList<Forum>) {
        mAdapter!!.bind(forumList)
    }

    override fun showThreadUi(fid: String) {
        ThreadListActivity.startActivity(getActivity(), fid)
    }

    override fun removeForum(forum: Forum) {
        mAdapter!!.remove(forum)
    }

    override fun onDestroy() {
        super.onDestroy()
        mPresenter!!.detachView()
    }

    override fun onForumDelAttentionClick(forum: Forum?) {
        mPresenter!!.onForumAttentionDelClick(forum)
    }

    override fun onForumOfflineClick(forum: Forum?) {
        mPresenter!!.onForumOfflineClick(forum)
    }

    override fun onForumClick(forum: Forum?) {
        mPresenter!!.onForumClick(forum)
    }

    companion object {

        fun newInstance(forumId: String): ForumListFragment {
            val mFragment = ForumListFragment()
            val bundle = Bundle()
            bundle.putString("forumId", forumId)
            mFragment.setArguments(bundle)
            return mFragment
        }
    }
}
